home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1996
/
MacHack 1996.toast
/
Hacks
/
Hacks ’87
/
Source ƒ.sit
/
Source ƒ
/
C ƒ
/
CITADEL BBS 'C' SRC
/
LIBTABL.C
< prev
next >
Wrap
C/C++ Source or Header
|
1987-01-14
|
4KB
|
143 lines
/************************************************************************/
/* libtabl.c */
/* Code to handle CTDLTABL.SYS */
/************************************************************************/
/************************************************************************/
/* history */
/* */
/* 86Apr24 HAW Modified for fwrite() and fread(). */
/* 85Nov15 HAW Created. */
/************************************************************************/
#include "ctdl.h"
/************************************************************************/
/* Contents */
/* */
/* readSysTab() restores system state from ctdltabl.sys */
/* writeSysTab() saves state of system in CTDLTABL.SYS */
/************************************************************************/
struct config cfg; /* A buncha variables */
struct lTable *logTab; /* RAM index of pippuls */
struct netTable *netTab; /* RAM index of nodes */
struct rTable roomTab[MAXROOMS]; /* RAM index of rooms */
char *indexTable = "ctdlTabl.sys";
static char *msg1 = "?old ctdlTabl.sys!";
FILE *safeopen();
char *malloc();
/************************************************************************/
/* readSysTab() restores state of system from CTDLTABL.SYS */
/* returns: TRUE on success, else FALSE */
/* destroys CTDLTABL.TAB after read, to prevent erroneous re-use */
/* in the event of a crash. */
/* */
/* MS-DOS fun: Here's the map -- */
/* Word 1 == sizeof cfg */
/* Word 2 == sizeof logTab */
/* Word 3 == sizeof roomTab */
/* Word 4 -- thru x == cfg contents */
/* x -- y == logTab */
/* y -- z == roomTab */
/* z -- a == netTab */
/* EOF */
/************************************************************************/
readSysTab(kill)
char kill;
{
unsigned int getw();
char eofflag = FALSE;
FILE *fd;
unsigned int size1, size2, size3;
if ((fd = safeopen(indexTable, "rb")) == NULL) {
printf("?no %s!", indexTable); /* Tsk, tsk! */
return(FALSE);
}
size1 = getw(fd);
eofflag = ferror(fd);
size2 = getw(fd);
eofflag = (eofflag) ? eofflag : ferror(fd);
size3 = getw(fd);
eofflag = (eofflag) ? eofflag : ferror(fd);
if (eofflag) {
printf(msg1);
return(FALSE);
}
logTab = (struct lTable *) malloc(size2);
/* "- 1" is kludge */
if (!common_read(&cfg, (sizeof cfg), 1, fd))
return FALSE;
if (size1 != sizeof cfg ||
size3 != sizeof roomTab ||
size2 != sizeof(*logTab) * cfg.MAXLOGTAB) {
printf(msg1);
return(FALSE);
}
if (!common_read(logTab, size2, 1, fd))
return FALSE;
if (!common_read(roomTab, sizeof roomTab, 1, fd))
return FALSE;
netTab = (struct netTable *) malloc(sizeof (*netTab) * cfg.netSize);
if (cfg.netSize)
if (!common_read(netTab, (sizeof(*netTab) * cfg.netSize), 1, fd))
return FALSE;
if (kill) unlink(indexTable);
return(TRUE);
}
/************************************************************************/
/* common_read() reads in from file the important stuff */
/* returns: TRUE on success, else FALSE */
/************************************************************************/
static common_read(block, size, elements, fd)
char *block;
unsigned size;
unsigned elements;
FILE *fd;
{
if (fread(block, size, elements, fd) != 1) {
printf(msg1);
return FALSE;
}
return TRUE;
}
/************************************************************************/
/* writeSysTab() saves state of system in CTDLTABL.SYS */
/* returns: TRUE on success, else ERROR */
/* See readSysTab() to see what the CTDLTABL.SYS map looks like. */
/************************************************************************/
writeSysTab()
{
unsigned char *c;
FILE *fd;
if ((fd = safeopen(indexTable, "wb")) == NULL) {
printf("?can't make %s", indexTable);
return(ERROR);
}
/* Write out some key stuff so we can detect bizarreness: */
putw(sizeof cfg , fd);
putw(sizeof(*logTab) * cfg.MAXLOGTAB, fd);
putw(sizeof roomTab, fd);
fwrite(&cfg, (sizeof cfg), 1, fd);
fwrite(logTab, (sizeof(*logTab) * cfg.MAXLOGTAB), 1, fd);
fwrite(roomTab, sizeof roomTab, 1, fd);
fwrite(netTab, (sizeof(*netTab) * cfg.netSize), 1, fd);
fclose(fd);
return(TRUE);
}